<?php
/***************************************
 * WIDGET-SUPPORT
 * To use the theme best, please use:
 * automattic.com/code/widgets/
 **************************************/

if (function_exists('register_sidebar')) {
	register_sidebar(array(
	 	'name' => 'Left',
        'before_widget' => '<div class="navi-item">',
        'after_widget' => '</div>',
        'before_title' => '<h3 class="title-navi">',
        'after_title' => '</h3>',
    ));
	register_sidebar(array(
	 	'name' => 'Center',
        'before_widget' => '<div class="navi-item">',
        'after_widget' => '</div>',
        'before_title' => '<h3 class="title-navi">',
        'after_title' => '</h3>',
    ));
	register_sidebar(array(
	 	'name' => 'Right',
        'before_widget' => '<div class="navi-item">',
        'after_widget' => '</div>',
        'before_title' => '<h3 class="title-navi">',
        'after_title' => '</h3>',
    ));
	register_sidebar(array(
	 	'name' => 'Sidebar',
        'before_widget' => '<div class="navi-item">',
        'after_widget' => '</div>',
        'before_title' => '<h3 class="title-navi">',
        'after_title' => '</h3>',
    ));
	register_sidebar(array(
	 	'name' => 'Single Right',
        'before_widget' => '<div class="navi-item">',
        'after_widget' => '</div>',
        'before_title' => '<h3 class="title-navi">',
        'after_title' => '</h3>',
    ));
	register_sidebar(array(
	 	'name' => 'Container',
        'before_widget' => '<div class="navi-item" style="float: left;">',
        'after_widget' => '</div><br style="clear: both" />',
        'before_title' => '<h3 class="title-navi">',
        'after_title' => '</h3>',
    ));
	register_sidebar(array(
	 	'name' => 'After Comments',
        'before_widget' => '<div class="navi-item" style="float: left;">',
        'after_widget' => '</div><br style="clear: both" />',
        'before_title' => '<h3 class="title-navi">',
        'after_title' => '</h3>',
    ));
}

// LINKS

function widget_simpax_links() {
?>
<div class="navi-item">
<ul>
<h3 class="title-navi">Links</h3>
<?php get_links('', '<li>', '</li>', '<br />', FALSE, 'rand', TRUE, 
TRUE, 5, TRUE); ?>
</ul>
</div>
<?php
}
if ( function_exists('register_sidebar_widget') )
    register_sidebar_widget('Links', 'widget_simpax_links');
	
// PAGES

function widget_simpax_pages() {
?>
<div class="navi-item">
<h3 class="title-navi"><?php _e('Seiten','simpax');?></h3>
<ul style="list-style-type:none;">
<?php wp_list_pages('sort_column=menu_order&depth=1&title_li='); ?>
</ul>
</div>
<?php
}
if ( function_exists('register_sidebar_widget') )
    register_sidebar_widget('Pages', 'widget_simpax_pages');
	
// SEARCH

function widget_simpax_search() {
?>
<div class="navi-item">
	<h3 class="title-navi"><?php _e('Suche','simpax');?></h3>
	<form method="get" id="searchform" action="<?php bloginfo('home'); ?>/">
	<div><input type="text" value="" name="s" id="s" />
	<input type="submit" id="searchsubmit" value="Suchen" />
	</div>
	</form>
</div>
<?php
}
if ( function_exists('register_sidebar_widget') )
    register_sidebar_widget('Search', 'widget_simpax_search');
	
// LIST CATEGORITIES
	
function widget_simpax_cats() {
?>
<div class="navi-item">	
<h3 class="title-navi"><?php _e('Kategorien','simpax');?></h3>
<ul>
<?php wp_list_cats('sort_column=name&optioncount=1&hide_empty=1'); ?>
</ul>
</div>
<?php
}
if ( function_exists('register_sidebar_widget') )
    register_sidebar_widget('Categorities', 'widget_simpax_cats');
	
// NEXT/PREV POST	
	
function widget_simpax_nextpost() {
	if( is_single() ) : ?>
<br style="clear: both" />
<p><?php next_post_link('%link &raquo;') ?></p>
<p><?php previous_post_link('&laquo; %link') ?></p>
<?php endif;
}
if ( function_exists('register_sidebar_widget') )
    register_sidebar_widget('Next/Prev Post', 'widget_simpax_nextpost');
	
// AJAX RSS WIDGET

function widget_simpax_ajaxrss($args, $number = 1) {
	extract($args);
	$options = get_option('widget_simpax_ajaxrss');
	$num_items = (int) $options[$number]['items'];
	$show_summary = $options[$number]['show_summary'];
	$url = $options[$number]['url'];
	if ( empty($url) )
		return;
	while ( strstr($url, 'http') != $url )
		$url = substr($url, 1);
	$title = $options[$number]['title'];
	if ( empty($title) )
		$title = __('Unknown Feed', 'widgets');
	$url = wp_specialchars(strip_tags($url), 1);
	$icon = get_bloginfo('stylesheet_directory').'/img/feed.png';
	$title = "<a class='rsswidget' href='$url'><img width='14' height='14' src='$icon' alt='RSS' /> $title</a>";
	echo $before_widget; 
	$title ? print($before_title . $title . $after_title) : null; 
	$num_items = (int) $options[$number]['items']; ?>
	<div id="widget_ajaxrss-<?php echo $number; ?>"><img src="<?php bloginfo('stylesheet_directory') ?>/img/loading.gif" alt="" /> loading...</div>
	<script type="text/javascript">
	$(document).ready(function(){
		$('#widget_ajaxrss-<?php echo $number; ?>').load('<?php bloginfo('stylesheet_directory'); ?>/js/ajax_rss.php?url=<?php echo $url ?>&num=<?php echo $num_items; ?>');
	});
	</script>
	<?php echo $after_widget;
}

function widget_simpax_ajaxrss_control($number) {
	$options = $newoptions = get_option('widget_simpax_ajaxrss');
	if ( $_POST["ajaxrss-submit-$number"] ) {
		$newoptions[$number]['items'] = (int) $_POST["ajaxrss-items-$number"];
		$newoptions[$number]['url'] = strip_tags(stripslashes($_POST["ajaxrss-url-$number"]));
		$newoptions[$number]['title'] = trim(strip_tags(stripslashes($_POST["ajaxrss-title-$number"])));
	}
	if ( $options != $newoptions ) {
		$options = $newoptions;
		update_option('widget_simpax_ajaxrss', $options);
	}
	$url = htmlspecialchars($options[$number]['url'], ENT_QUOTES);
	$items = (int) $options[$number]['items'];
	$title = htmlspecialchars($options[$number]['title'], ENT_QUOTES);
	if ( empty($items) || $items < 1 ) $items = 10;
?>
			<p style="text-align:center;"><?php _e('Enter the RSS feed URL here:', 'widgets'); ?></p>
			<input style="width: 400px;" id="ajaxrss-url-<?php echo "$number"; ?>" name="ajaxrss-url-<?php echo "$number"; ?>" type="text" value="<?php echo $url; ?>" />
			<p style="text-align:center;"><?php _e('Give the feed a title (optional):', 'widgets'); ?></p>
			<input style="width: 400px;" id="ajaxrss-title-<?php echo "$number"; ?>" name="ajaxrss-title-<?php echo "$number"; ?>" type="text" value="<?php echo $title; ?>" />
			<p style="text-align:center; line-height: 30px;"><?php _e('How many items would you like to display?', 'widgets'); ?> <select id="ajaxrss-items-<?php echo $number; ?>" name="ajaxrss-items-<?php echo $number; ?>"><?php for ( $i = 1; $i <= 10; ++$i ) echo "<option value='$i' ".($items==$i ? "selected='selected'" : '').">$i</option>"; ?></select></p>
			<input type="hidden" id="ajaxrss-submit-<?php echo "$number"; ?>" name="ajaxrss-submit-<?php echo "$number"; ?>" value="1" />
<?php
}

function widget_simpax_ajaxrss_page() {
	$options = $newoptions = get_option('widget_simpax_ajaxrss');
?>
	<div class="wrap">
		<form method="POST">
			<h2><?php _e('AJAX RSS Feed Widgets', 'widgets'); ?></h2>
			<p style="line-height: 30px;"><?php _e('How many RSS widgets would you like?', 'widgets'); ?>
			<select id="ajaxrss-number" name="ajaxrss-number" value="<?php echo $options['number']; ?>">
<?php for ( $i = 1; $i < 10; ++$i ) echo "<option value='$i' ".($options['number']==$i ? "selected='selected'" : '').">$i</option>"; ?>
			</select>
			<span class="submit"><input type="submit" name="ajaxrss-number-submit" id="ajaxrss-number-submit" value="<?php _e('Save'); ?>" /></span></p>
		</form>
	</div>
<?php
}

function widget_simpax_ajaxrss_head() {
}

function widget_simpax_ajaxrss_setup() {
	$options = $newoptions = get_option('widget_simpax_ajaxrss');
	if ( isset($_POST['ajaxrss-number-submit']) ) {
		$number = (int) $_POST['ajaxrss-number'];
		if ( $number > 9 ) $number = 9;
		if ( $number < 1 ) $number = 1;
		$newoptions['number'] = $number;
	}
	if ( $options != $newoptions ) {
		$options = $newoptions;
		update_option('widget_simpax_ajaxrss', $options);
		widget_simpax_ajaxrss_register($options['number']);
	}
}

function widget_simpax_ajaxrss_register() {
	$options = get_option('widget_simpax_ajaxrss');
	$number = $options['number'];
	if ( $number < 1 ) $number = 1;
	if ( $number > 9 ) $number = 9;
	for ($i = 1; $i <= 9; $i++) {
		$name = array('AJAX RSS %s', 'widgets', $i);
		register_sidebar_widget($name, $i <= $number ? 'widget_simpax_ajaxrss' : /* unregister */ '', $i);
		register_widget_control($name, $i <= $number ? 'widget_simpax_ajaxrss_control' : /* unregister */ '', 410, 200, $i);
	}
	add_action('sidebar_admin_setup', 'widget_simpax_ajaxrss_setup');
	add_action('sidebar_admin_page', 'widget_simpax_ajaxrss_page');

	if ( is_active_widget('widget_simpax_ajaxrss') )
		add_action('wp_head', 'widget_simpax_ajaxrss_head');
}

widget_simpax_ajaxrss_register();

// RECENT COMMENTS

function widget_simpax_recent_comments($args) {
	global $wpdb, $comments, $comment;
	extract($args, EXTR_SKIP);
	$options = get_option('widget_simpax_recent_comments');
	$title = empty($options['title']) ? __('Cax Recent Comments', 'widgets') : $options['title'];
	$comments = $wpdb->get_results("SELECT comment_author, comment_author_url, comment_ID, comment_post_ID FROM $wpdb->comments WHERE comment_approved = '1' ORDER BY comment_date_gmt DESC LIMIT 5");
?>

		<?php echo $before_widget; ?>
			<?php echo $before_title . $title . $after_title; ?>
			<ul id="recentcomments"><?php
			if ( $comments ) : foreach ($comments as $comment) :
			echo  '<li class="recentcomments"><a href="'. get_permalink($comment->comment_post_ID) . '#comment-' . $comment->comment_ID . '">'.get_comment_author().''.__(' bei ', simpax).'' . get_the_title($comment->comment_post_ID) . '</a></li>';
			endforeach; endif;?></ul>
		<?php echo $after_widget; ?>
<?php
}

function widget_simpax_recent_comments_control() {
	$options = $newoptions = get_option('widget_simpax_recent_comments');
	if ( $_POST["simpax-recent-comments-submit"] ) {
		$newoptions['title'] = strip_tags(stripslashes($_POST["simpax-recent-comments-title"]));
	}
	if ( $options != $newoptions ) {
		$options = $newoptions;
		update_option('widget_simpax_recent_comments', $options);
	}
	$title = htmlspecialchars($options['title'], ENT_QUOTES);
?>
			<p><label for="simpax-recent-comments-title"><?php _e('Title:'); ?> <input style="width: 250px;" id="simpax-recent-comments-title" name="simpax-recent-comments-title" type="text" value="<?php echo $title; ?>" /></label></p>
			<input type="hidden" id="simpax-recent-comments-submit" name="simpax-recent-comments-submit" value="1" />
<?php
}

function widget_simpax_recent_comments_style() {
?>
<style type="text/css">.recentcomments a{display:inline !important;padding: 0 !important;margin: 0 !important;}</style>
<?php
}

function widget_simpax_recent_comments_register() {
	register_sidebar_widget(array('Cax Recent Comments', 'widgets'), 'widget_simpax_recent_comments');
	register_widget_control(array('Cax Recent Comments', 'widgets'), 'widget_simpax_recent_comments_control', 300, 90);
	
	if ( is_active_widget('widget_simpax_recent_comments') )
		add_action('wp_head', 'widget_simpax_recent_comments_style');
}

widget_simpax_recent_comments_register();

?>